package vcp.server.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.joda.time.DateTime;

import vcp.entity.WeekStatistics;
import vcp.server.DBAdapter;
import vcp.server.DAO.interfaces.IGenericDAO;
import vcp.server.utility.Constants.DayStatisticsTable;
import vcp.server.utility.Constants.TableNames;
import vcp.server.utility.Constants.WeeklyStatisticsTable;
import vcp.server.utility.SQLQueries;

public class WeekStatisticsDAO implements IGenericDAO <WeekStatistics, Void>
{
	// ****************************************************************************************************************************
	// IGenericDAO methods
	// ****************************************************************************************************************************
	@Override
	public Integer findMaxID ()
	{
		Integer weekStatisticsID = null;
		String query =
				String.format(
					SQLQueries.SELECT_MAX__VALUE__AS__NAME__FROM__TABLE_NAME__
					, DayStatisticsTable.ID
					, SQLQueries.MAX_ID
					, TableNames.WEEKLY_STATISTICS);

		try
		{
			ResultSet set = DBAdapter.getAdapter().find(query);
			if ( set.next() )
			{
				weekStatisticsID = set.getInt("maxID");
			}
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}

		if ( weekStatisticsID == null )
		{
			weekStatisticsID = 0;
		}

		return ++ weekStatisticsID;
	}

	@Override
	public WeekStatistics create ()
	{
		return new WeekStatistics();
	}

	@Override
	public WeekStatistics findByEntity ( Void entity )
	{
		return null;
	}

	@Override
	public WeekStatistics findByID ( int ID )
	{
		return this.findByID(String.valueOf(ID));
	}

	@Override
	public WeekStatistics findByID ( String ID )
	{
		String query =
				String.format(
					SQLQueries.SELECT_ALL_FROM__SINGLETABLE__WHERE__COMPARE_
					, TableNames.WEEKLY_STATISTICS
					, WeeklyStatisticsTable.ID + "=" + ID);

		WeekStatistics weekStatistics = null;
		try
		{
			ResultSet set = DBAdapter.getAdapter().find(query);
			if ( set.next() )
			{
				weekStatistics = this.create();

				weekStatistics.setID(set.getInt(WeeklyStatisticsTable.ID));
				weekStatistics.setStartDate(new DateTime(set.getTimestamp(WeeklyStatisticsTable.START_DATE)));
				weekStatistics.setEndDate(new DateTime(set.getTimestamp(WeeklyStatisticsTable.END_DATE)));
			}
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}

		return weekStatistics;
	}

	@Override
	public List <WeekStatistics> findAll ( Void info )
	{
		return null;
	}

	@Override
	public boolean save ( WeekStatistics entity )
	{
		boolean result = false;

		String valuesSet =
				String.format(
					"%d,\"%s\",\"%s\"",
					entity.getID()
					, entity.getStartDate() != null
													? new Timestamp(entity.getStartDate().getMillis())
													: new Timestamp(new DateTime(2001, 1, 1, 0, 0, 0).getMillis())
					, entity.getEndDate() != null
													? new Timestamp(entity.getEndDate().getMillis())
													: new Timestamp(new DateTime(2001, 1, 1, 0, 0, 0).getMillis()));

		String sqlQuery =
				String.format(
					SQLQueries.INSERT_INTO__TABLENAME__VALUES__
					, TableNames.WEEKLY_STATISTICS
					, valuesSet);

		try
		{
			result = DBAdapter.getAdapter().save(sqlQuery);
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public boolean refresh ( String sqlQuery )
	{
		return false;
	}

	@Override
	public boolean deleteByID ( int ID )
	{
		return false;
	}

	@Override
	public boolean deleteByID ( String ID )
	{
		return false;
	}

}
